//
//  MBCommodityChannelsModel.swift
//  StarTimesON
//
//  Created by Toj on 9/25/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

class MBCommodityChannelsModel: NSObject {
    // 后端返回的状态
    private(set) var payShow = false
    // 后端返回的数据
    private(set) var channels: [STChannelCellModel] = []
    private(set) var showChannels: [STChannelCellModel] = []
    
    private(set) var walletChannel: STChannelCellModel?
    
    private(set) var defaultChannel: STChannelCellModel?
    
    var sectionIndex: Int = 0
    
    // 选中的规格
    public var selectedChannel: STChannelCellModel? { willSet {
        selectedChannel?.isSelected = false
        newValue?.isSelected = true
    } }
    
    // 当前状态
    var isOpened: Bool = false { didSet {
        if isOpened {
            showChannels = channels
            return
        }
        
        guard let channel = selectedChannel else {
            showChannels = channels
            return
        }
        guard let dependChannel = channel.dependChannel else {
            showChannels = [channel]
            return
        }
        showChannels = [dependChannel, channel]
    } }
    
    // 根据当前状态返回列表数量
    var showNumber: Int {
        return showChannels.count
    }
    
    // 选择规格后 重置状态, 以后端为准
    func reset() {
        selectedChannel = defaultChannel
        isOpened = payShow
        guard channels.count <= 1 else { return }
        defaultChannel?.isOnlyOne = true
    }
    
    init(commodityChannel: CommodityChannel, allPayChannels: [String: PayChannelDto]) {
        super.init()
        
        let payChannels = commodityChannel.payChannels
        var channelViewModels: [STChannelCellModel] = []
        var defaultChannel_: STChannelCellModel?
        let isLogin = STUserModel.shared.isLogin()
        payChannels?.forEach { channel in
            let channelID = channel.id
            guard let payChannel = allPayChannels[channelID] else { return }
            
            // 非绑卡支付方式
            guard payChannel.isSupportCardBind && isLogin else {
                let channelViewModel = STChannelCellModel(payChannel: payChannel, channel: channel)
                channelViewModels.append(channelViewModel)
                if defaultChannel_ == nil { defaultChannel_ = channelViewModel }
                if channelViewModel.isEWallet { walletChannel = channelViewModel }
                return
            }
            
            // 绑卡支付方式
            let channelViewModel = STChannelCellModel(payChannel: payChannel, channel: channel)
            channelViewModels.append(channelViewModel)
            if let list = payChannel.payChannelCardAuthDtoList, list.count > 0 {
                let cardArr = payCardToViewModel(payChannelCards: list, channelModel: channelViewModel)
                if defaultChannel_ == nil { defaultChannel_ = cardArr.first }
                channelViewModels += cardArr
            }
            // Add支付方式
            channelViewModels.append(addCarViewModel(channel: channel, payChannel: payChannel))
        }
        
        payShow = commodityChannel.payShow
        channels = channelViewModels
        defaultChannel = defaultChannel_
    }
    
    func payCardToViewModel(payChannelCards: [PayChannelCardAuthDto], channelModel: STChannelCellModel) -> [STChannelCellModel] {
        var channels:[STChannelCellModel] = []
        payChannelCards.forEach { dto in
            let channel = STChannelCellModel(payChannelCard: dto, channelModel: channelModel)
            channels.append(channel)
        }
        return channels
    }
    
    func addCarViewModel(channel: CommodityPayChannel, payChannel: PayChannelDto) -> STChannelCellModel {
        let addCarCell = STChannelCellModel(payChannel: payChannel, channel: channel)
        addCarCell.isAddCarCell = true
        addCarCell.isCarCell = true
        addCarCell.name = "add_another_card_pay".localized()
        return addCarCell
    }
    
    func updateWalletChannelInfo() {
        walletChannel?.eWalletAccount(STSPayAccount.current.payAccount)
    }
}
